package org.elanfox.intellicommunity.msg.server.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.elanfox.intellicommunity.common.utils.HexUtils;
import org.elanfox.intellicommunity.msg.dto.*;
import org.elanfox.intellicommunity.msg.ienum.CmdEnums;
import org.elanfox.intellicommunity.msg.server.base.MsgCommunicationUtils;
import org.elanfox.intellicommunity.msg.server.base.WriteResult;
import org.elanfox.intellicommunity.msg.server.service.CmdSetWriter;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/3/13
 */
@Service
@Slf4j
public class CmdSetWriterImpl implements CmdSetWriter {
    @Override
    public WriteResult lfScope(CmdWriteParam cmdWriteParam) {
        CmdLfScopeWriteParam writeParam = (CmdLfScopeWriteParam) cmdWriteParam;
        //构建请求参数
        String hexData = HexUtils.numToHex8(writeParam.getScope());
        String hexLength = "0007";

        return MsgCommunicationUtils.request(writeParam.getHexAddr(), CmdEnums.Code.LF_SCOPE, hexData, hexLength);
    }

    @Override
    public WriteResult rssiFilter(CmdWriteParam cmdParam) {
        CmdRssiFilterWriteParam writeParam = (CmdRssiFilterWriteParam) cmdParam;
        String hexLength;
        String hexData;
        if (writeParam.isEnabled()) {
            hexLength = "0008";
            hexData = "01" + HexUtils.numToHex8(writeParam.getLimitVal());
        } else {
            hexLength = "0007";
            hexData = "00";
        }

        return MsgCommunicationUtils.request(writeParam.getHexAddr(), CmdEnums.Code.RSSI_FILTER, hexData, hexLength);
    }

    //    @Override
//    public void rename(CmdWriteParam cmdParam) {
//        CmdRenameWriteParam renameParam = (CmdRenameWriteParam) cmdParam;
//        String hexData = HexUtils.stringToHex(renameParam.getDeviceName());
//        int length = hexData.length() / 2 + 6;
//        String hexLength = HexUtils.numToHex16(length);
//
//        MsgCommunicationUtils.request(renameParam.getHexAddr(), CmdEnums.Code.NAME, hexData, hexLength);
//    }
//
//    @Override
//    public void netCommunication(CmdWriteParam writeParam) {
//        CmdNetComWriteParam netComWriteParam = (CmdNetComWriteParam) writeParam;
//        String hexPort = HexUtils.numToHex16(netComWriteParam.getPort());
//        String hexServerIp = HexUtils.ipToHex(netComWriteParam.getServerIp());
//        String hexServerPort = HexUtils.numToHex16(netComWriteParam.getServerPort());
//        String hexTimes = HexUtils.numToHex8(netComWriteParam.getRetryTimes());
//        String hexInterval = HexUtils.numToHex16(netComWriteParam.getRetryInterval());
//        String hexInterval2 = HexUtils.numToHex16(netComWriteParam.getRetryIntervalTerminal());
//
//        //默认TCP client模式
//        String hexData = "00" + hexPort + hexServerIp + hexServerPort + hexTimes + hexInterval + hexInterval2;
//        String hexLength = "14";
//
//        MsgCommunicationUtils.request(netComWriteParam.getHexAddr(), CmdEnums.Code.NET_COMMUNICATION, hexData, hexLength);
//    }
//
//    @Override
//    public void net(CmdWriteParam writeParam) {
//        CmdNetWriteParam netWriteParam = (CmdNetWriteParam) writeParam;
//        String hexIp = HexUtils.ipToHex(netWriteParam.getIp());
//        String hexMask = HexUtils.ipToHex(netWriteParam.getSubnetMask());
//        String hexGateWay = HexUtils.ipToHex(netWriteParam.getGateway());
//        String hexDns = HexUtils.ipToHex(netWriteParam.getDns());
//
//        String hexData = hexIp + hexMask + hexGateWay + hexDns;
//
//        String hexLength = "42";
//
//        MsgCommunicationUtils.request(writeParam.getHexAddr(), CmdEnums.Code.NET, hexData, hexLength);
//    }
//
    @Override
    public WriteResult buzzerStatus(CmdWriteParam writeParam) {
        CmdBuzzerStatusWriteParam buzzerStatusWriteParam = (CmdBuzzerStatusWriteParam) writeParam;
        String hexLength = "000A";
        //消息体
        String hexData = buzzerStatusWriteParam.isEnabled() ? "01" : "00";

        return MsgCommunicationUtils.request(buzzerStatusWriteParam.getHexAddr(), CmdEnums.Code.BUZZER_STATUS, hexData, hexLength);
    }

    @Override
    public WriteResult deviceAddr(CmdWriteParam writeParam) {
        CmdAddrWriteParam addrWriteParam = (CmdAddrWriteParam) writeParam;
        String hexLength = "0008";
        String hexData = addrWriteParam.getStationType().getHexCode() + HexUtils.numToHex8(addrWriteParam.getNo());

        return MsgCommunicationUtils.request(addrWriteParam.getHexAddr(), CmdEnums.Code.DEVICE_ADDR, hexData, hexLength);
    }

    @Override
    public WriteResult filterTime(CmdWriteParam writeParam) {
        CmdFilterWriteParam filterWriteParam = (CmdFilterWriteParam) writeParam;
        Assert.isTrue(filterWriteParam.getInterval() >= 0 && filterWriteParam.getInterval() <= 30, "间隔取值范围为0~30");
        String hexLength = "0007";
        String hexData = HexUtils.numToHex8(filterWriteParam.getInterval());

        return MsgCommunicationUtils.request(filterWriteParam.getHexAddr(), CmdEnums.Code.FILTER_WAITING_TIME, hexData, hexLength);
    }

    @Override
    public WriteResult write(CmdEnums.Code cmdCode, CmdWriteParam arg) {
        switch (cmdCode) {
            case LF_SCOPE:
                return lfScope(arg);
            case RSSI_FILTER:
                return rssiFilter(arg);
//            case NAME:
//                rename(arg);
//                break;
//            case NET_COMMUNICATION:
//                netCommunication(arg);
//                break;
//            case NET:
//                net(arg);
//                break;
            case BUZZER_STATUS:
                return buzzerStatus(arg);
            case DEVICE_ADDR:
                return deviceAddr(arg);
            case FILTER_WAITING_TIME:
                return filterTime(arg);
            default:
                log.error("method not supported for cmd code " + cmdCode.getDesc());
                return null;
        }
    }
}
